/**
 * 
 */
package com.laozhao.EasySales.business;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.laozhao.EasySales.model.entity.SysSql;
import com.laozhao.dreamframe.dbmanager.DBFactory;
import com.laozhao.dreamframe.dbmanager.DataBaseManager;
import com.laozhao.dreamframe.model.ModelUtil;

/**
 * @author LaoZhao
 *
 */
public class SqlServer extends AbstractServer {
	private transient Log log = LogFactory.getLog(this.getClass());
	
	private HashMap<String, SysSql> sqlMap ;
	
	public SqlServer(){
		init();
	}
	
	@Override
	public void init(){
		super.init();
		sqlMap = new HashMap<String, SysSql>();
		initMap();
	}
	
	private void initMap() {
		log.debug("init sql map start ");
		DataBaseManager dmmgr = DBFactory.getInstance();
		List<SysSql> list = null;
		try {
			Connection conn = dmmgr.openConnect();
			conn.setAutoCommit(false);
			Statement statement = conn.createStatement();
			ResultSet rs = statement.executeQuery("select * from sys_sql");
//			dmmgr.u
			list = ModelUtil.Relection(rs, SysSql.class);
			conn.commit();
			log.debug("get sql list success ");
		} catch (SQLException e) {
			log.error("Failed get sql list");
			log.debug("Failed message:" , e);
			e.printStackTrace();
		}
		
		for (SysSql sysSql : list) {
			if (sysSql != null && sysSql.getSqlId() != null) {
				sqlMap.put(sysSql.getSqlId(), sysSql);
			}
		}
		
	}

	public SysSql getSql(String key){
		if (sqlMap.containsKey(key)) {
			return sqlMap.get(key);
		}else {
			return null;
		}
	}


	public String getSqlValue(String key){
		if (sqlMap.containsKey(key)) {
			return sqlMap.get(key).getSqlValue();
		}else {
			return null;
		}
	}

}
